<?php

namespace api\wxapp\controller;

use cmf\controller\RestBaseController;
use think\Db;
use wxapp\aes\WXBizDataCrypt;

class MessageController extends RestBaseController
{

    //填写表格 表格填写通知
    public function msge1(){

        $token=$this->gettoken();

        $template_id='-5D9CBr3OyEKA_1Nbgs27mZbxDxfhnOmTgW6gKIE2mw';

        $touser='o1WFK5VFXWisZbacMNZ_TyiZbyEM';

        $data['thing1']=['value'=>'报单名称','color'=>'#00008B'];
        $data['thing4']=['value'=>'请填写快递单号','color'=>'#00008B'];
        $data['time3']=['value'=>date('Y-m-d H:i'),'color'=>'#00008B'];

        sendMessage($token,$template_id,$touser,$data);

    }

    //填写表格 表格填写通知
    public function msge2(){

        $token=$this->gettoken();

        $template_id='DprgOzUXL3IQ-3vY8RrO98pEcrAgKlbWV2TofwK9Pe4';

        $touser='o1WFK5VFXWisZbacMNZ_TyiZbyEM';

        $data['thing2']=['value'=>'请填写转寄单号','color'=>'#00008B'];
        $data['thing4']=['value'=>'请尽快处理','color'=>'#00008B'];
        $data['time3']=['value'=>date('Y-m-d H:i'),'color'=>'#00008B'];

        sendMessage($token,$template_id,$touser,$data);

    }

    //获取token
    public function gettoken(){

        $wxappSettings = cmf_get_option('wxapp_settings');
        $defaultWxapp = $wxappSettings['default'];

        $appid = $defaultWxapp['app_id'];
        $appsecret    = $defaultWxapp['app_secret'];

        $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appid . '&secret=' . $appsecret;


        $res =$this->http_requestget($url);

        $access_token =json_decode($res,true);
        return $access_token['access_token'];


    }

    //发送 get 请求
    function http_requestget($url){


        $curl = curl_init();
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_TIMEOUT, time());
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_URL, $url);
        $res = curl_exec($curl);
        if (curl_errno($curl)) {
            echo 'Error GET '.curl_error($curl);
        }
        curl_close($curl);

        return $res;//返回结果
    }





}